#include "../common/net.hpp"
#include "../common/dispatcher.hpp"
#include "../client/requestor.hpp"
#include "../client/rpc_caller.hpp"
#include <thread>

void callback(const Json::Value &result)
{
    ILOG("callback result：%d",result.asInt());
}
int main()
{
    auto requestor=std::make_shared<myrpc::client::Requestor>();
    auto caller=std::make_shared<myrpc::client::RpcCaller>(requestor);

    auto dispatcher=std::make_shared<myrpc::Dispatcher>();
    auto rsp_cb=std::bind(&myrpc::client::Requestor::onResponse,requestor.get(),std::placeholders::_1,std::placeholders::_2);
    dispatcher->registerHandler<myrpc::BaseMessage>(myrpc::MType::RSP_RPC,rsp_cb);

    auto client=myrpc::ClientFactory::create("127.0.0.1",9090);
    auto message_cb=std::bind(&myrpc::Dispatcher::onMessage,dispatcher.get(),std::placeholders::_1,std::placeholders::_2);
    client->setMessageCallback(message_cb);
    client->connect();

    auto conn=client->connection();
    Json::Value params,result;
    params["num1"]=11;
    params["num2"]=22;

    bool ret=caller->call(conn,"Add",params,result);
    if(ret!=false)
    {
        std::cout<<result.asInt()<<std::endl;
    }

    myrpc::client::RpcCaller::JsonAsyncResponse res_future;

    params["num1"]=333;
    params["num2"]=333;

    ret=caller->call(conn,"Add",params,res_future);
    if(ret!=false)
    {
        result=res_future.get();
        std::cout<<result.asInt()<<std::endl;
    }
   
    params["num1"]=333;
    params["num2"]=666;

    ret=caller->call(conn,"Add",params,callback);
    DLOG("----");
    std::this_thread::sleep_for(std::chrono::seconds(1));
    client->shutdown();
    return 0;
}